package com.changli05.compare.comparable;

public class Product implements Comparable{
    String name;
    int price;

    public Product() {
    }

    public Product(String name, int price) {
        this.name = name;
        this.price = price;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getPrice() {
        return price;
    }

    public void setPrice(int price) {
        this.price = price;
    }

    @Override
    public String toString() {
        return "Product{" +
                "name='" + name + '\'' +
                ", price=" + price +
                '}';
    }

//    @Override
//    public int compareTo(Object o) {
//        if(this == o){
//            return 0;
//        }
//        if(o instanceof Product){
//            Product p = (Product)o;
//            return Double.compare(this.price, p.getPrice());//升序
//            //return Double.compare(p.getPrice(),this.price );//降序
//        }
//        throw  new RuntimeException("类型不匹配！！！");
//    }
    @Override
    public int compareTo(Object o) {
        if(this == o){
            return 0;
        }
        if(o instanceof Product){
            Product p = (Product)o;
            int value = Double.compare(this.price, p.getPrice());//升序
            if(value == 0){
                return this.name.compareTo(p.name);
            }
            return value;

        }
        throw  new RuntimeException("类型不匹配！！！");
    }
}
